<template>
  <basic-container>
    <el-form ref="queryForm"
             :model="queryParams"
             :inline="true"
             label-width="80px">
      <el-form-item label="任务名称"
                    prop="searchParams.search_eq_jobName">
        <el-input v-model="queryParams.searchParams.search_eq_jobName"
                  placeholder="请输入角色名称"/>
      </el-form-item>
      <el-form-item label="任务组"
                    prop="searchParams.search_eq_jobGroup">
        <el-select v-model="queryParams.searchParams.search_eq_jobGroup" placeholder="请选择"  @change="selectChange">
          <el-option
                  v-for="dict in jobGroupOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态"
                    prop="searchParams.search_eq_status">
        <el-select v-model="queryParams.searchParams.search_eq_status" placeholder="请选择">
          <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   icon="el-icon-search"
                   size="mini"
                   @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh"
                   size="mini"
                   @click="resetQuery">重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10"
            class="mb8">
      <el-col :span="1.5">
        <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
        >清空
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="warning"
                icon="el-icon-download"
                size="mini"
        >导出
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading"
              :data="userList"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="jobLogId" width="80"/>
      <el-table-column label="任务名称" align="center" prop="jobName"/>
      <el-table-column label="任务组名" align="center" prop="jobGroup" width="80" :formatter="groupFormat"/>
      <el-table-column label="调用目标字符串" align="center" prop="invokeTarget" show-overflow-tooltip="true"/>
      <el-table-column label="使用状态" align="center" prop="status" width="80" :formatter="statusFormat"/>
      <el-table-column label="日志信息" align="center" prop="jobMessage" show-overflow-tooltip="true"/>
      <el-table-column label="执行时间" align="center"  :formatter="dFormat"/>
      <el-table-column label="异常信息" align="center" prop="exceptionInfo" show-overflow-tooltip="true"/>
    </el-table>

    <pagination v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

  </basic-container>
</template>

<script>
  import { delJobLog,delAllJobLog, listJobLog} from "@/api/monitor/jobLog";
  import {dateFormat} from '@/util/date'

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 用户信息表格数据
        userList: [],
        jobGroupOptions: [],
        concurrentOptions: [],
        misfirePolicyOptions: [],
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          searchParams:{
            search_eq_jobName: undefined,
            search_eq_jobGroup: undefined,
            search_eq_status: undefined,
          }
        },
      }

    },
    created() {
      this.getList();
      this.getDicts("job_group").then(response=>{
        this.jobGroupOptions =response.data.data;
      });
      this.getDicts("job_disallow").then(response=>{
        this.concurrentOptions =response.data.data;
      });
      this.getDicts("job_misfire_policy").then(response=>{
        this.misfirePolicyOptions =response.data.data;
      });
      this.getDicts("job_status").then(response=>{
        this.statusOptions =response.data.data;
      });
    },
    methods: {
      /** 搜索按钮操作 */
      handleQuery() {
        for (let key in this.queryParams.searchParams) {
          if(this.queryParams.searchParams[key]!=undefined){
            if (this.queryParams.searchParams[key] == "") {
              this.queryParams.searchParams[key] = undefined;
              continue;
            }
            if(this.queryParams.searchParams[key].length>0){
              if(!this.queryParams.searchParams[key].constructor == Array){
                if(!(this.queryParams.searchParams[key].replace(" ", "").trim().length > 0)){
                  this.queryParams.searchParams[key] = undefined;
                  continue;
                }
              }
            }
          }
        }
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 查询收费员信息列表 */
      getList() {
        this.loading = true;
        listJobLog(this.queryParams).then(response => {
          const data = response.data;
          if (response.status===200&&data.code === "00") {
            this.userList = data.data.rows;
            this.total = data.data.total;
          } else {
            this.msgError(data.msg);
          }
          this.loading = false;
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        // this.ids = selection.map(item => item.id)
        this.ids = selection;
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const userIds = row.id || this.ids;
        let tt = '';
        if (row.id == undefined) {
          tt = '是否确认删除【' + this.ids.length + '】条数据吗?';
        } else {
          tt = '是否确认删除为"' + row.dictName + '"的数据项?';
        }
        this.$confirm(tt, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delJobLog(userIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 删除按钮操作 */
      handleAllDelete() {
        this.$confirm('是否确认清空所有的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delAllJobLog();
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      selectChange(value) {
        this.queryParams.searchParams.search_eq_jobGroup = value;
        this.getList();
      },
      groupFormat(row) {
        return this.selectDictLabel(this.jobGroupOptions,row.jobGroup);
      },
      statusFormat(row) {
        return this.selectDictLabel(this.statusOptions,row.status);
      },
      dFormat(row) {
        if (row.createTime == null) {
          return "/";
        }
        return dateFormat(new Date(row.createTime));
      }
    }
  };
</script>

<style>

</style>
